package com.ruoyi.web.task;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.sql.LambdaQueryWrapperX;
import com.ruoyi.liangshi.domain.ArchItem;
import com.ruoyi.liangshi.domain.BizKcKsc;
import com.ruoyi.liangshi.domain.BsOrder;
import com.ruoyi.liangshi.service.IArchItemService;
import com.ruoyi.liangshi.service.IBizKcKscService;
import com.ruoyi.liangshi.service.IBsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author LiuJin
 * @Date：Create in 2023-11-19 17:47
 * @Description 订单中菜品信息数据同步定时任务
 */
@Slf4j
@Component
public class OrderKscTask {

    @Autowired
    private IBsOrderService bsOrderService;

    @Autowired
    private IBizKcKscService kcKscService;

    @Autowired
    private IArchItemService archItemService;

    /**
     * 用于计算订单的实际耗时
     * 订单耗时 = Max（菜品的耗时信息）
     */
    @Scheduled(cron = "0/10 * * * * ?")
    private void orderKscDataSync() {
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapperX<BsOrder> wrapper  = new LambdaQueryWrapperX<BsOrder>();
        wrapper.eq(BsOrder::getCalcStatus, "0")
               // .between(BsOrder::getOpenTime, now.with(LocalTime.MIN), now.with(LocalTime.MAX))
                .last("limit 100");
        //查询结算时间不为空，且同步状态不等于2的订单信息
        List<BsOrder> orderList = bsOrderService.list(wrapper);
        //获取所有的菜品信息， 更新菜品的分类
        Map<Long ,ArchItem> archItemMap = archItemService.selectAtchItemMap();
        for (BsOrder bsOrder : orderList) {
            LambdaQueryWrapperX<BizKcKsc> wrapperKsc  = new LambdaQueryWrapperX<BizKcKsc>();
            wrapperKsc.eq(BizKcKsc::getBsId,bsOrder.getId());
            List<BizKcKsc> kcKscList = kcKscService.list(wrapperKsc);
            Integer timeLim = 1 ;
            Integer bsKcCount = 0 ; //  菜品数量
            Integer kcCount_A = 0 ; //  菜品数量A
            Integer kcCount_B = 0  ; //  菜品数量B
            Integer kcCount_C = 0  ; //  菜品数量C
            for (BizKcKsc item:kcKscList){
                if(item.getKitchenFlg()!=3){
                    timeLim= 1;
                    continue;
                }
                Integer orderCallTime =  item.getOrderCallTime();
                if(orderCallTime.compareTo(timeLim)>0){
                    timeLim = orderCallTime ;
                }
                ArchItem archItem =archItemMap.get(item.getItemId());
                if(archItem!=null&&ObjectUtil.equal("A",archItem.getAtchType())){
                    kcCount_A++ ;
                }else if(archItem!=null&&ObjectUtil.equal("B",archItem.getAtchType())){
                    kcCount_B++ ;
                }else if(archItem!=null&&ObjectUtil.equal("C",archItem.getAtchType())){
                    kcCount_C++ ;
                }
                bsKcCount ++  ;
            }
            bsOrder.setBsKcCount(bsKcCount);
            bsOrder.setBsKcCountA(kcCount_A);
            bsOrder.setBsKcCountB(kcCount_B);
            bsOrder.setBsKcCountC(kcCount_C);
            bsOrder.setOrderCallTime(timeLim.longValue());
            if(bsOrder.getState()==1){
                bsOrder.setCalcStatus(2L); // 设置为已经计算数据
            }
            if(bsOrder.getState()==3){
                bsOrder.setOrderCallTime(-1L);
                bsOrder.setCalcStatus(2L); // 设置为已退款的
            }
            bsOrderService.updateById(bsOrder);
        }
    }

    /**
     * 计算菜品的耗时信息
     * 菜品耗时等于 开始时间和 传菜时间的时间间隔
     */
    @Scheduled(cron = "0/60 * * * * ?")
    private void kscCallTimeUpdate() {
        LambdaQueryWrapperX<BizKcKsc> wrapper  = new LambdaQueryWrapperX<BizKcKsc>();
        wrapper.eq(BizKcKsc::getKitchenFlg,"3")
                .isNull(BizKcKsc::getOrderCallTime)
                .orderByAsc(BizKcKsc::getCallUpTime)
                .last("limit 10");
        List<BizKcKsc> kcKscList = kcKscService.list(wrapper);
        // 更新订单计时状态
        for (BizKcKsc item:kcKscList){
            Date callUpTime = item.getCallUpTime();
            Date serveFinishTime = item.getServeFinishTime();
            // 得到菜品的开始时间和实际上菜时间的时间间隔，作为菜品的加工时长
            Long timeLim = Math.abs(serveFinishTime.getTime()- callUpTime.getTime())/1000;
            log.info("菜品的开始时间{}，菜品的完成时间{},时间间隔{}",callUpTime,serveFinishTime,timeLim);
            item.setOrderCallTime(timeLim.intValue());
            kcKscService.updateBizKcKsc(item);
        }
    }


}
